package com.example.boardroom.serial.table.ModbusRtu;


import android.annotation.SuppressLint;

import com.blankj.utilcode.util.LogUtils;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 进制转换工具类
 */
public class ParseSystemUtil {

    /**
     * 将二进制转换成16进制
     *
     * @param buf
     * @return
     */
    public static String parseByte2HexStr(byte[] buf) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < buf.length; i++) {
            String hex = Integer.toHexString(buf[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            sb.append(hex.toUpperCase());
        }
        return sb.toString();
    }

//    /**
//     * 将16进制转换为二进制
//     *
//     * @param hexStr
//     * @return
//     */
//    public static byte[] parseHexStr2Byte(String hexStr) {
//        if (hexStr.length() < 1)
//            return null;
//        byte[] result = new byte[hexStr.length() / 2];
//        for (int i = 0; i < hexStr.length() / 2; i++) {
//            int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
//            int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2),
//                    16);
//            result[i] = (byte) (high * 16 + low);
//        }
//        return result;
//    }

    /**
     * 16进制转2进制
     *
     * @param hex
     * @return
     */
    public static String hexStringToByte(String hex) {
//        int i = Integer.parseInt(hex, 16);
        long l = Long.parseLong(hex, 16);
        return Long.toBinaryString(l);
    }

    /**
     * JAVA 10进制转16进制高位在前低位在后
     *
     * @param var0
     * @return
     */
    private static String lowHigh(int var0) {
        int var1 = 1;
        int var2 = var0 >> 8;
        int var3 = var0 & 255;
        String var4 = Integer.toHexString(var2);
        String var5 = Integer.toHexString(var3);
        if (var4.length() > 2) {
            do {
                if (var1 > 1) {
                    var2 >>= 8;
                }
                var4 = Integer.toHexString(var2 >> 8);
                var5 = var5 + Integer.toHexString(var2 & 255);
                ++var1;
            } while (var4.length() > 2);
        }
        if (var4.length() < 2) {
            var4 = "0" + var4;
        }
        if (var5.length() < 2) {
            var5 = "0" + var5;
        }
        return var5 + var4;
    }

    /**
     * JAVA 10进制转16进制低位在前高位在后
     *
     * @param dec
     * @return
     */
    public static String decToHex(int dec) {
        String hex = "";
        while (dec != 0) {
            String h = Integer.toString(dec & 0xff, 16);
            if ((h.length() & 0x01) == 1)
                h = '0' + h;
            hex = hex + h;
            dec = dec >> 8;
        }
        return hex;
    }

//https://www.cnblogs.com/bingoj/p/11148305.html

    /**
     * 单精度浮点数转换
     *
     * @param hexStr 16位浮点数
     * @return 10进制
     */
    public static double transfer(String hexStr) {
        StringBuilder binaryStr = new StringBuilder();
        for (int i = 0; i < hexStr.length(); i += 2) {
            String a = hexStr.substring(i, i + 2);
            int c = Integer.parseInt(a, 16);
            @SuppressLint("DefaultLocale") String item = String.format("%08d", Integer.parseInt(Integer.toBinaryString(c)));
            binaryStr.append(item);
        }
        int n = Integer.parseInt(binaryStr.substring(1, 9), 2);
        String mStr = binaryStr.substring(9, binaryStr.length() - 1);
        double sum = 0;
        for (int i = 1; i <= mStr.length(); i++) {
            if (mStr.charAt(i - 1) == '1') {
                sum = sum + Math.pow(0.5, i);
            }
        }
        return (Math.pow(2, n - 127)) * (1 + sum);
    }

    /**
     * @param: [content]
     * @return: int
     * @description: 十六进制转十进制
     */
    public static int covert(String content) {
        int number = 0;
        String[] HighLetter = {"A", "B", "C", "D", "E", "F"};
        Map<String, Integer> map = new HashMap<>();
        for (int i = 0; i <= 9; i++) {
            map.put(i + "", i);
        }
        for (int j = 10; j < HighLetter.length + 10; j++) {
            map.put(HighLetter[j - 10], j);
        }
        String[] str = new String[content.length()];
        for (int i = 0; i < str.length; i++) {
            str[i] = content.substring(i, i + 1);
        }
        for (int i = 0; i < str.length; i++) {
            number += map.get(str[i]) * Math.pow(16, str.length - 1 - i);
        }
        return number;
    }


    //  public static void main(String[] args) throws ParseException {
    //        String format = "HH:mm:ss";
    //        Date nowTime = new SimpleDateFormat(format).parse("09:27:00");
    //        Date startTime = new SimpleDateFormat(format).parse("09:27:00");
    //        Date endTime = new SimpleDateFormat(format).parse("09:27:59");
    //        System.out.println(isEffectiveDate(nowTime, startTime, endTime));
    //    }
    //
    //    /**
    //     * 判断当前时间是否在[startTime, endTime]区间，注意时间格式要一致
    //     *
    //     * @param nowTime 当前时间
    //     * @param startTime 开始时间
    //     * @param endTime 结束时间
    //     * @return
    //     * @author jqlin
    //     */
    //    public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
    //        if (nowTime.getTime() == startTime.getTime()
    //                || nowTime.getTime() == endTime.getTime()) {
    //            return true;
    //        }
    //
    //        Calendar date = Calendar.getInstance();
    //        date.setTime(nowTime);
    //
    //        Calendar begin = Calendar.getInstance();
    //        begin.setTime(startTime);
    //
    //        Calendar end = Calendar.getInstance();
    //        end.setTime(endTime);
    //
    //        if (date.after(begin) && date.before(end)) {
    //            return true;
    //        } else {
    //            return false;
    //        }
    //    }
    //}

    /**
     * 判断当前时间是否在[startTime, endTime]区间，注意时间格式要一致
     *
     * @param nowTime   当前时间
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return
     * @author jqlin
     */
    public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {

        boolean timeCompareSize = DateUtils.getTimeCompareSize(DateUtils.dateToStrLong(endTime), DateUtils.dateToStrLong(startTime));

        if (!timeCompareSize) {
            Calendar cal = Calendar.getInstance();
            cal.setTime(endTime);
            cal.add(Calendar.HOUR_OF_DAY, 24);//24小时制
            endTime= cal.getTime();
        }

        LogUtils.dTag("lufeidate",nowTime,startTime,endTime);
        if (nowTime.getTime() == startTime.getTime()
                || nowTime.getTime() == endTime.getTime()) {
            return true;
        }

        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(startTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        return date.after(begin) && date.before(end);
    }

    /**
     * @param a 数字1
     * @param b 数字2
     * @return 数字1是否小于数字2
     */
    public static boolean lessThan(String a, int b) {
//        return (Integer.parseInt(a)<b) || a != null;
        if (null==a){
            return false;
        }
        return Integer.parseInt(a) < b;
    }
    /**
     * @param a 数字1
     * @param b 数字2
     * @return 数字1是否大于数字2
     */
    public static boolean greaterThan(String a, int b) {
//        return (a == b) || (a != null && a.equals(b));
        if (null==a){
            return false;
        }
        return Integer.parseInt(a) > b;
    }

    /**
     * @param a 数字1
     * @param b 数字2
     * @return 数字1等于数字2
     */
    public static boolean beEqualTo(String a, int b) {
//        return (a == b) || (a != null && a.equals(b));
        if (null==a){
            return false;
        }
        return Integer.parseInt(a) == b;
    }

    /**

     * 毫秒数转换成天时分秒

     *

     * @param milliseconds

     */

    public static StringBuilder millisecondsConvertToDHMS(long milliseconds) {
        String daysStr;
        String hoursStr;
        String minutesStr;
        String secondsStr;
        //天
        long day = (milliseconds / 1000) / (24 * 3600);
        if (day < 10) {
            daysStr = "0" + day;
        } else {
            daysStr = day + "";
        }
        //时
        long hour = ((milliseconds / 1000) % (24 * 3600)) / 3600;
        if (hour < 10) {
            hoursStr = "0" + hour;
        } else {
            hoursStr = hour + "";
        }
        //分
        long minute = ((milliseconds / 1000) % 3600) / 60;
        if (minute < 10) {
            minutesStr = "0" + minute;
        } else {
            minutesStr = minute + "";
        }
        //秒
        long second = (milliseconds / 1000) % 60;
        if (second < 10) {
            secondsStr = "0" + second;
        } else {
            secondsStr = second + "";
        }
        StringBuilder stringBuilder=new StringBuilder();
        stringBuilder.append(daysStr);
        stringBuilder.append("天");
        stringBuilder.append(hoursStr);
        stringBuilder.append("小时");
        stringBuilder.append(minutesStr);
        stringBuilder.append("分钟");
        stringBuilder.append(secondsStr);
        stringBuilder.append("秒");
        return stringBuilder;
    }
}